import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:study_online/entity/Student.dart';

class Global {
  static Global _instance;
  Dio dio;
  String token;
  int num=0;
  Student user = new Student(
      '201901020111',
      'test',
      'pwd',
      '计科',
      '男',
      'https://img2.baidu.com/it/u=172917783,2425047487&fm=26&fmt=auto',
      88,
      '15344425756');

  static Global getInstance() {
    if (_instance == null) _instance = new Global();
    return _instance;
  }

  Global() {
    dio = new Dio();
    dio.options = BaseOptions(
      //baseUrl: "http://lliuqu.top:8080/study_online",
      baseUrl: "http://192.168.43.173:8088",
      //baseUrl: "http://192.168.0.5:8088",
      //baseUrl: "http://192.168.30.1:8088",
      connectTimeout: 5000,
      sendTimeout: 5000,
      receiveTimeout: 10000,
      contentType: Headers.formUrlEncodedContentType,
      responseType: ResponseType.json,
    );
    dio.interceptors.add(InterceptorsWrapper(
      onRequest: (options, handler) {
        EasyLoading.show(
          status: "Loading...",
          //maskType: EasyLoadingMaskType.black,
        );
        return handler.next(options); //continue
      },
      onResponse: (response, handler) {
        EasyLoading.dismiss();
        return handler.next(response); // continue
      },
      onError: (DioError e, handler) {
        print(e.toString());
        EasyLoading.dismiss();
        String msg = "";
        if (e.type == DioErrorType.connectTimeout) {
          msg = "连接超时错误";
        } else {
          msg = "接口错误！";
        }
        EasyLoading.showError(msg);
      },
    ));
  }
}
